import request from '@/router/axios';
import {getToken} from '@/util/auth';
import {baseUrl} from '@/config/env';
import website from '@/config/website'
const saveLog = website.saveLog ||false;

export const loginByUsername = (tenantId, username, password, type) => request({
  url: baseUrl + '/auth/token',
  method: 'post',
  headers: {
    'Tenant-Code': tenantId
  },
  params: {
    tenantId,
    username,
    password,
    grant_type: "password",
    scope: "all",
    type
  }
})

export const loginBySocial = (tenantId, source, code, state) => request({
  url: baseUrl + '/auth/token',
  method: 'post',
  headers: {
    'Tenant-Id': tenantId
  },
  params: {
    tenantId,
    source,
    code,
    state,
    grant_type: "social",
    scope: "all",
  }
})

export const refreshToken = (refresh_token) => request({
	url: baseUrl + '/auth/token',
	method: 'post',
	params: {
		refresh_token,
		grant_type: "refresh_token",
		scope: "all",
	}
})

export const getUserDetail = () => request({
	url: baseUrl + '/user/detail',
	method: 'get'
});
export const getUserInfo = () => {
  return request({
    url: baseUrl+'/user/info',
    method: 'get',
  })
}
export const updateInfo = (row) => {
	return request({
		url: baseUrl + '/user/update',
		method: 'post',
		data: row
	})
}

export const updatePassword = (pwdOld, pwdNew) => {
	return request({
		url: baseUrl + '/user/update-password',
		method: 'post',
		params: {
			pwdOld: pwdOld,
			pwdNew: pwdNew
		}
	})
}
export const register = (row) => {
	return request({
		url: baseUrl + '/auth/register',
		method: 'post',
		data: row
	})
}

export const logout = () => request({
	url: baseUrl + '/auth/logout',
	method: 'get'
});


// export const getButtons = () => request({
//   url: baseUrl +'/api/blade-system/menu/buttons',
//   method: 'get'
// });


// export const getUserMenu = () => request({
// 	url: baseUrl + '/user/menu',
// 	method: 'get'
// });
// export const getUserMenuButton = () => request({
// 	url: baseUrl + '/user/menu-button',
// 	method: 'get'
// });
export const getUserMenu = () => {}
export const saveMenuLog = () => {}

export const saveButtonLog = (params) => {
	if(!saveLog){
		return false;
	}
	return request({
		url: baseUrl +"/log/button",
		method: 'post',
		timeout: 100,
		headers:{ "LogApi":"LogApi"},
		data: params
	});
}
export const getLogList = (current, size, params) => {
	return request({
		url: baseUrl + "/log/list",
		method: 'get',
		params: {
			...params,
			current,
			size,
		}
	})
}

export const uploadFile = (file) => {
	const formData = new FormData();
	formData.append('file', file);
	return request({
		timeout: 30000,
		url: baseUrl + '/file/upload',
		method: 'post',
		data: formData
	})
}

export const downLoadFile = (id) => {
	let url = baseUrl + '/file/downLoadFile?id=' + id + '&Blade-Auth=' + getToken();
	window.open(url);
}

//菜单导航接口
export const navigationApi = () => request({
	url: baseUrl+'/ccid-system/user/user-menu',
	method: 'get',
})
